<?php 

class My_Model extends Zend_Db_Table_Abstract {
	
	public $emptyToNull = array();
	
	// przyjmijmy że to tylko update block (przeciewieństwo colsToEdit)
	public $editBlock = array();// TODO zastanowic sie czy tego używac 
			
	/**
	 * ta metoda ma zwracac obiekt klasy validującej model Valid_ModelClassName extend Validator
	 * zinstancjonowny z $this jako pierwszym argumentem
	 */
	public function getValidator(){
		$vName = 'Valid_'.get_class($this);
		$fName = APPLICATION_PATH . '/../library/Valid/'.get_class($this).'.php';
		if(file_exists($fName) && class_exists($vName)){// GREAT Success!
			return new $vName($this);
		} else {
			return false;
		}
	}
	
	public function _q(array $c){
		
		$k = function ($key,$def = null) use($c){
			return array_key_exists($key, $c) ? $c[$key] : $def;
		};
		
		$where = $k('where');
		$join = $k('join');
		$order = $k('order');
		$limit = $k('limit');
		$start = $k('start',0);
		$adapter = $k('adapter');
		$group = $k('group');
		$having = $k('having');
		$fields = $k('fields');
		
		return $this->q($where,$join,$order,$limit,$start,$adapter,$group,$having,$fields);
	}
	
		
	
	/**
	 * @param unknown_type $where
	 * @param unknown_type $join - pamiętaj że # oznacza główną tabele zapytania
	 * @param unknown_type $order
	 * @param unknown_type $limit
	 * @param unknown_type $start
	 * @return Zend_Db_Statement_Interface
	 */
	public function q(
		$where, $join = null, $order = null,$limit = null, $start = 0, 
		$adapter = null,$group = null,$having = null,$fields = null,$log = null){
		
		$db = $adapter == null ? $this->getAdapter() : $adapter;
		$table_name = $this->info('name');
		$sel = $db->select();
		
		if(!$fields){
			$sel->from($table_name);
		} else {
			$sel->from($table_name,$fields);
		}
		
		$process_join = function ($j) use ($sel,$table_name){
						
			list($table,$on,$flds) = $j;
			
			if(strpos($on,'#') !== false){
				$on = str_replace('#', $table_name, $on);
			}
			
			if(isset($j[3]) && $j[3]=='left'){
				$sel->joinLeft($table,$on,$flds);
			} else {
				$sel->join($table,$on,$flds);	
			}
			
		};
		
		if(is_array($join)){
			if(array_key_exists('joins', $join)){// TODO cos tu niedziałao - albo źle skonfigowałem - 
				foreach ($join["joins"] as $j){
					//Validation::react(json_encode($join));
					$process_join($j);
				}
			} else {
				$process_join($join);
			}
		}
				
		if($where)$sel->where($where);
		if($order)$sel->order($order);
		if($limit)$sel->limit($limit, $start);
		if($group)$sel->group($group);
		if($having)$sel->having($having);
		
		try {
			//if($this->info('name')=='salary_exception')Utils::logVeryHard($sel->assemble());
				$log && Utils::logVeryHard($sel->assemble());
			return $db->query($sel);	
		} catch (Exception $e){
			Utils::logVeryHard($sel->assemble());
			Validation::react($sel->assemble());
		}
	}
	
	public function updateById($data,$id = null){
		$pk = Utils::arrayFetch($this->info('primary'));
		$rq = Zend_controller_Front::getInstance()->getRequest();
		($id === null) && ($id = $rq->getParam($pk));
		$this->update($data,Utils::quote($pk.' = ?',$id));
	}
}
